/**
 * @packageDocumentation
 * @module api.functional
 * @nestia Generated by Nestia - https://github.com/samchon/nestia
 */
//================================================================
import type {
  IConnection,
  IPropagation,
  Resolved,
  HttpError,
} from "@nestia/fetcher";
import { NestiaSimulator } from "@nestia/fetcher/lib/NestiaSimulator";
import { PlainFetcher } from "@nestia/fetcher/lib/PlainFetcher";
import typia from "typia";

export * as user from "./user";

/**
 * @controller AppController.getHello
 * @path GET /:param
 * @nestia Generated by Nestia - https://github.com/samchon/nestia
 */
export async function getHello(
  connection: IConnection,
  param: string,
): Promise<getHello.Output> {
  typia.assert<typeof param>(param);
  return !!connection.simulate
    ? getHello.simulate(connection, param)
    : PlainFetcher.propagate(connection, {
        ...getHello.METADATA,
        template: getHello.METADATA.path,
        path: getHello.path(param),
      });
}
export namespace getHello {
  export type Output = IPropagation<{
    200: string;
  }>;

  export const METADATA = {
    method: "GET",
    path: "/:param",
    request: null,
    response: {
      type: "application/json",
      encrypted: false,
    },
    status: null,
  } as const;

  export const path = (param: string) =>
    `/${encodeURIComponent(param ?? "null")}`;
  export const random = (
    g?: Partial<typia.IRandomGenerator>,
  ): Resolved<string> => typia.random<string>(g);
  export const simulate = (connection: IConnection, param: string): Output => {
    const assert = NestiaSimulator.assert({
      method: METADATA.method,
      host: connection.host,
      path: path(param),
      contentType: "application/json",
    });
    try {
      assert.param("param")(() => typia.assert(param));
    } catch (exp) {
      if (!typia.is<HttpError>(exp)) throw exp;
      return {
        success: false,
        status: exp.status,
        headers: exp.headers,
        data: exp.toJSON().message,
      } as any;
    }
    return {
      success: true,
      status: 200,
      headers: {
        "Content-Type": "application/json",
      },
      data: random(
        "object" === typeof connection.simulate && null !== connection.simulate
          ? connection.simulate
          : undefined,
      ),
    };
  };
}
